//
//  AFNetTableViewController.m
//  objective_c_language
//
//  Created by 龙 on 2018/5/25.
//  Copyright © 2018年 longlong. All rights reserved.
//
#define CellIdentifer @"UITableViewCell"

#import "AFNetTableViewController.h"
#import "PercentEscaped.h"
#import <AFNetworking/AFNetworking.h>

@interface AFNetTableViewController ()
@property (nonatomic ,strong) NSArray<NSString *> *titleArray;
@end

@implementation AFNetTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifer];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer forIndexPath:indexPath];
    cell.textLabel.text = self.titleArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0)
    {
        [HttpManager requestCityListSuccess:^(NSMutableArray<CityListModel *> *cityArray) {
            
        } failure:^(NSError *error) {
            
        }];

    }
    else if (indexPath.row == 1)
    {
        [self percentEscapedClick];
    }
    else if (indexPath.row == 2)
    {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeDeterminate;
        hud.label.text = @"上传文件...";
        [HttpManager requestBigFileUploadTestProgress:^(NSProgress * _Nonnull progress) {
            
            hud.progressObject = progress;
            
            if (hud.progress >= 1)
            {
                [hud hideAnimated:YES];
            }
            
        }];
    }else if (indexPath.row == 3)
    {
        //AFNetworking 是如何发送一个 request，又是如何 接收数据的呢？它的工作流程是什么呢？
        //我们以一个简答的 GET 请求为例，来探究它的工作流程；
        //这是一个简单的从 itunes 获取 id=1164001330 的app信息的请求，
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager GET:@"http://itunes.apple.com/lookup?id=1164001330" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"responseObject --- %@",responseObject);
            [manager.session invalidateAndCancel];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [manager.session invalidateAndCancel];
            NSLog(@"error --- %@",error);
        }];
        //首先分析它的第一行代码，如何生成一个 AFHTTPSessionManager
        //我们可以清楚的看到，不论我们采用何种方法初始化一个 AFHTTPSessionManager 实例，最终它都调用
    }
}

#pragma marl - PercentEscaped Method

- (void)percentEscapedClick
{
    NSString *name = @"zhangsan";
    NSString *name1 = @"张三";
    NSString *name2 = @"lisi";
    NSString *escaped = PercentEscapedStringFromString(name);//%E5%BC%A0%E4%B8%89
    NSString *escaped1 = PercentEscapedStringFromString(name1);//zhangsan

    
    NSDictionary *dict1 = @{@"name":name,@"age":@20};
    NSDictionary *dict2 = @{@"name":name1,@"age":@22};
    NSDictionary *dict3 = @{@"name":name2,@"age":@23};
    NSString *dictString1 = QueryStringFromParameters(dict1);//age=20&name=zhangsan
    NSString *dictString2 = QueryStringFromParameters(dict2);//age=22&name=%E5%BC%A0%E4%B8%89


    NSDictionary *bigDict = @{@"userInfo":@[dict1,dict3],@"name":name,@"dict":dict1};
    NSString *bigDictString = QueryStringFromParameters(bigDict);
    /*
     dict%5Bage%5D=20&dict%5Bname%5D=zhangsan&name=zhangsan&userInfo%5B%5D%5Bage%5D=20&userInfo%5B%5D%5Bname%5D=zhangsan&userInfo%5B%5D%5Bage%5D=23&userInfo%5B%5D%5Bname%5D=lisi

     dict[age]=20&dict[name]=zhangsan& name=zhangsan& userInfo[][age]=20&userInfo[][name]=zhangsan&userInfo[][age]=23&userInfo[][name]=lisi
     */
    NSLog(@"bigDictString ----- %@",bigDictString);
}

#pragma mark - setter and getter

- (NSArray<NSString *> *)titleArray
{
    if (_titleArray == nil)
    {
        _titleArray = @[@"get请求",@"URL编码",@"上传大文件",@"内存泄漏"];
    }
    
    return _titleArray;
}
@end
